<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-12 17:11
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class findOrderStatisticsCriteria extends BaseCriterias
{
	private $params;

	public function __construct($storeId, $status, $barberId = null, $isOffline = false, $date = null)
	{
		$this->params = [
			'store_id'   => $storeId,
			'barber_id'  => $barberId,
			'status'     => $status,
			'date'       => $date,
			'is_offline' => $isOffline
		];
	}

	public function apply($model, RepositoryInterface $repository)
	{
		$map['status'] = $this->params['status'];
		$map['store_id'] = $this->params['store_id'];
		empty($this->params['barber_id']) ? : $map['barber_id'] = $this->params['barber_id'];
		$opt = $this->params['is_offline'] ? '=' : '!=';
		$date = empty($this->params['date']) ? date('Y-m-d'):  $this->params['date'];
//		$year = 0;$month=0;
//		if ($isSetDate) {
//			$date = explode('-', $this->params['date']);
//			$year = $date[0];
//			$month = $date[1];
//		}

		return $model
			->where($map)
			->where('source',$opt,config('constant.appointment.store'))
			->whereDate('created_at',$date);
//			->when($isSetDate, function ($query) use ($year, $month) {
//				$query->whereYear('created_at', $year)->whereMonth('created_at', $month);
//			});
	}
}